package basketball

import (
	global2 "longmen/server/config/global"
	basketball_behaviour2 "longmen/server/internal/services/nami/api/v5/sports/basketball/basketball_behaviour"
	"longmen/server/internal/services/nami/config"
	"longmen/server/internal/services/nami/services/sports/basketball"
	"longmen/server/internal/services/nami/utils/restyclient"
	"longmen/server/pkg/db/mysql/models/models_sports"
	"time"
)

type BasketBall struct {
}

func (b BasketBall) SyncCategory() {
	resp := basketball_behaviour2.CategoryApiResponse{}
	err := restyclient.GetUrlAndBindRecords(config.BBCategoryUrl+config.BuildParamsAndUserAndSecret(), &resp)
	if err != nil {
		global2.Log.Error("获取篮球赛事分类失败: ", err)
		return
	}
	// 调用入库
	basketball.CrateCategory(resp.Interface())
}

func (b BasketBall) SyncCountry() {
	resp := basketball_behaviour2.CountryApiResponse{}
	err := restyclient.GetUrlAndBindRecords(config.BBCountryUrl+config.BuildParamsAndUserAndSecret(), &resp)
	if err != nil {
		global2.Log.Error("获取篮球赛事分类失败: ", err)
		return
	}
	// 调用入库
	basketball.CrateCountry(resp.Interface())
}

func (b BasketBall) SyncCompetition() {
	resp := basketball_behaviour2.CompetitionApiResponse{}
	maxId := basketball.GetCompetitionId()
	queryStr := config.BuildParamsAndUserAndSecret(map[string]interface{}{
		"id":    maxId,
		"limit": 1000,
	})
	err := restyclient.GetUrlAndBindRecords(config.BBCompetitionUrl+queryStr, &resp)
	if err != nil {
		global2.Log.Error("获取篮球赛事列表失败: ", err)
		return
	}
	// 调用入库
	basketball.CrateCompetition(resp.Interface())
}

func (b BasketBall) SyncTeam() {
	resp := basketball_behaviour2.TeamApiResponse{}
	maxId := basketball.GetMaxId()
	global2.Log.Debugf("篮球球队同步id:%v", maxId)
	queryStr := config.BuildParamsAndUserAndSecret(map[string]interface{}{
		"id":    maxId,
		"limit": 1000,
	})
	err := restyclient.GetUrlAndBindRecords(config.BBTeamsUrl+queryStr, &resp)
	if err != nil {
		global2.Log.Error("获取篮球球队列表失败: ", err)
		return
	}
	// 调用入库
	basketball.CrateTeam(resp.Interface())
}

// SyncMatch 获取赛程赛果列表-日期查询
// 该接口返回所请求日期的全量赛程赛果数据（请求限制：前后30天）
// 注：实时数据的获取通过实时统计数据接口
//
// 当天赛程 建议请求频次：10min/次（全量更新）
func (b BasketBall) SyncMatch() {
	// 当天赛程
	today := time.Now().Format("20060102")
	syncMatch(today)
}

func syncMatch(today string) {
	resp := basketball_behaviour2.MatchApiResponse{}
	queryStr := config.BuildParamsAndUserAndSecret(map[string]interface{}{
		"date": today,
	})
	err := restyclient.GetUrlAndBindRecords(config.BBMatchDiaryUrl+queryStr, &resp)
	if err != nil {
		global2.Log.Error("获取篮球赛程列表失败: ", err)
		return
	}
	// 调用入库
	basketball.CrateMatch(resp.Interface())
}

// SyncFutureMatch 获取赛程赛果列表-日期查询
// 该接口返回所请求日期的全量赛程赛果数据（请求限制：前后30天）
// 注：实时数据的获取通过实时统计数据接口
//
// 未来赛程 建议请求频次：30min/次（全量更新）
func (b BasketBall) SyncFutureMatch() {
	for i := 1; i <= 7; i++ {
		formatDay := time.Now().AddDate(0, 0, i).Format("20060102")
		syncMatch(formatDay)
	}
}

func (b BasketBall) SyncMatchByDate(date string) {
	syncMatch(date)
}

func (b BasketBall) SyncLive() {
	//panic("implement me")
}

// SyncDelete 获取删除数据
// 返回最近24小时内删除的数据id（比赛、球队、球员、赛事、赛季、阶段），需定时同步
//
// 建议请求频次：1~5min/次
func (b BasketBall) SyncDelete() {
	resp := basketball_behaviour2.DeleteApiResponse{}
	queryStr := config.BuildParamsAndUserAndSecret(map[string]interface{}{})
	err := restyclient.GetUrlAndBindRecords(config.BBDelete+queryStr, &resp)
	if err != nil {
		global2.Log.Error("获取删除列表失败: ", err)
		return
	}
	// 调用入库
	if len(resp.Results.Team) > 0 {
		basketball.DeleteTeams(resp.Results.Team)
	}

	if len(resp.Results.Match) > 0 {
		basketball.DeleteMatch(resp.Results.Match)
		global2.Sport().Where("match_id in ?", resp.Results.Match).Delete(models_sports.MatchFavorite{})
		global2.Sport().Where("match_id in ?", resp.Results.Match).Delete(models_sports.MatchFavoriteStatistics{})
	}

	if len(resp.Results.Competition) > 0 {
		basketball.DeleteCompetition(resp.Results.Competition)
	}
}

var basketBallRecentId int

// SyncRecent 获取变动比赛列表
// 用于获取比赛变动后的数据（比赛时间、状态等），可根据时间戳增量获取新增或变动的数据
// 限制：前30天比赛开始
//
// 1、首次全量更新，根据参数id获取全量数据
// 2、后续增量更新，根据参数time增量获取变动数据（建议请求频次：1min/次）
func (b BasketBall) SyncRecent() {
	resp := basketball_behaviour2.RecentApiResponse{}
	queryStr := config.BuildParamsAndUserAndSecret(map[string]interface{}{
		"id":    basketBallRecentId,
		"limit": 1000,
	})
	err := restyclient.GetUrlAndBindRecords(config.BBRecentChange+queryStr, &resp)
	if err != nil {
		global2.Log.Error("获取更新列表失败: ", err)
		return
	}
	basketball.UpdateMatchAllField(resp.UpdateMatchInterFaces())
	if resp.Query.Total != 1000 {
		basketBallRecentId = 0
	} else {
		basketBallRecentId = resp.Query.MaxID
	}
}
